﻿using System;

using System.Windows.Forms;

namespace Eflong
{
    public enum SysPriority { None = 0 , Normal = 1 , Admin = 2 };

    public partial class LoginForm : Form
    {
        public string UserId { get; set; }

        public bool Logined { get; set; }

        public SysPriority Priority { get; set; }

        private bool _isQuit = true;

        /// <summary>
        /// 判断是登录界面或退出界面
        /// </summary>
        public bool IsQuit
        {
            get { return _isQuit; }
            set
            {
                _isQuit = value;
            }
        }

        public LoginForm( )
        {
            InitializeComponent( );

            tbUserId.KeyDown += new KeyEventHandler( tbUserId_KeyDown );
            tbPassword.KeyDown += new KeyEventHandler( tbPassword_KeyDown );
            Logined = false;
        }

        private void tbPassword_KeyDown( object sender , KeyEventArgs e )
        {
            if ( e.KeyData == Keys.Enter )
            {
                if ( btnQuit.Visible )
                {
                    btnQuit.Select( );
                }
            }
        }

        private void tbUserId_KeyDown( object sender , KeyEventArgs e )
        {
            if ( e.KeyData == Keys.Enter )
            {
                UserId = tbUserId.Text.Trim( );
                tbPassword.Select( );
            }
        }

        public bool IsLogin( )
        {
            if ( tbUserId.Text.ToLower( ) == "eflong" && tbPassword.Text == "eflong1234" )
            {
                Logined = true;
                UserId = tbUserId.Text.Trim( );
                Priority = SysPriority.Admin;
            }
            else if ( tbUserId.Text.ToLower( ) == "admin" && tbPassword.Text == "admin" )
            {
                Logined = true;
                UserId = tbUserId.Text.Trim( );
                Priority = SysPriority.Normal;
            }
            else
            {
                Priority = SysPriority.None;
                Logined = false;
            }
            return Logined;
        }

        private void btnQuit_Click( object sender , EventArgs e )
        {
            if ( IsLogin( ) )
            {
                if ( IsQuit )
                {
                    Quit( );
                }
            }
            else
            {
                MessageBox.Show( this , "您输入的用户名或口令不正确、\n请重新输入!" , "错误提示" , MessageBoxButtons.OK , MessageBoxIcon.Information );
            }
        }

        /// <summary>
        /// 退出提示

        /// </summary>
        protected virtual void Quit( )
        {
            if ( MessageBox.Show( this , "您想退出系统吗?" , "关闭提示" , MessageBoxButtons.YesNo , MessageBoxIcon.Information ) == DialogResult.Yes )
            {
                this.DialogResult = DialogResult.Yes;
                this.Close( );
            }
            else
            {
                btnQuit.DialogResult = DialogResult.None;
            }
        }

        private void tbUserId_TextChanged( object sender , EventArgs e )
        {
        }

        private void LoginForm_Load( object sender , EventArgs e )
        {
            if ( !_isQuit )
            {
                btnQuit.Text = "登录系统";
            }
            else
            {
                btnQuit.Text = "退出系统";
            }
            Priority = SysPriority.None;
        }
    }
}